package org.elanfox.intellicommunity.common.utils;

import org.elanfox.intellicommunity.common.constant.Constant;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @author guomw
 * @date 2018/11/14
 */
public class ImageUtils {

    /**
     * 生成用户证书
     *
     * @param sourceImgPath 源图片地址
     * @param uerName       姓名
     * @param levelName     代理级别
     * @param wechat        微信号
     * @param mobile        手机号码
     * @param date          授权日期
     * @return
     * @throws IOException
     */
    public static BufferedImage buildCertImage(String sourceImgPath, String uerName, String levelName, String wechat, String mobile, String date) throws IOException {

        if (StringUtils.isEmpty(uerName)) {
            uerName = "";
        }
        if (StringUtils.isEmpty(levelName)) {
            levelName = "";
        }
        if (StringUtils.isEmpty(wechat)) {
            wechat = "";
        }
        if (StringUtils.isEmpty(mobile)) {
            mobile = "";
        }
        if (StringUtils.isEmpty(date)) {
            date = "";
        }
        // 读取原图片信息
        File srcImgFile = ResourceUtils.getFile(sourceImgPath);
        //文件转化为图片
        Image srcImg = ImageIO.read(srcImgFile);
        //获取图片的宽
        int srcImgWidth = srcImg.getWidth(null);
        //获取图片的高
        int srcImgHeight = srcImg.getHeight(null);
        // 加水印
        BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bufImg.createGraphics();
        g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
        //根据图片的背景设置水印颜色
        Font font = new Font("微软雅黑", Font.PLAIN, 22);
        g.setColor(new Color(128, 0, 0));
        //设置字体
        g.setFont(font);

        int y = 500;
        //画出水印
        String name=new String(uerName.getBytes(Constant.ENCODING_UTF8));
        g.drawString(name, 310, y + 35);
        g.drawString(new String(levelName.getBytes(Constant.ENCODING_UTF8)), 350, y + 78);
        g.drawString(wechat, 330, y + 120);
        g.drawString(mobile, 330, y + 165);
        g.drawString(date, 415, srcImgHeight - 180);

        //g.draws
        g.dispose();

        Graphics2D g2 = bufImg.createGraphics();
        g2.drawImage(bufImg, 0, 0, srcImgWidth, srcImgHeight, null);
        Font font2 = new Font("微软雅黑", Font.PLAIN, 26);
        g2.setColor(new Color(216, 209, 156));
        //设置水印文字的旋转角度
        Integer degree = 45;
        //设置水印透明度
        //设置字体
        g2.setFont(font2);

        //设置水印旋转
        g2.rotate(Math.toRadians(degree));

        JLabel label = new JLabel(name);
        FontMetrics metrics = label.getFontMetrics(font);
        //文字水印的宽
        int width = metrics.stringWidth(label.getText())+100;
        // 图片的高  除以  文字水印的宽    ——> 打印的行数(以文字水印的宽为间隔)
        int rowsNumber = srcImgHeight/width;
        int columnsNumber = srcImgWidth/width;
        //防止图片太小而文字水印太长，所以至少打印一次
        if(rowsNumber < 1){
            rowsNumber = 1;
        }
        if(columnsNumber < 1){
            columnsNumber = 1;
        }
        for(int j=0;j<rowsNumber;j++){
            for(int i=0;i<columnsNumber;i++){
                g2.drawString(name, i*width + j*width+100, -i*width + j*width+50);
                //画出水印,并设置水印位置
            }
        }
        g2.dispose(); // 输出图片
        return bufImg;
    }
}
